import sys
import os
import pandas as pd

root = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(root)

from main_v2 import tools
import time

data = os.path.join(root,"datasets")

def get_clarity(input_file_list):
    images = []
    with open(input_file_list,"r") as op:
        for line in op.readlines():
            images.append(line.split()[0])

    if not os.path.exists(os.path.join(root, "outputs")):
        os.makedirs(os.path.join(root, "outputs"))

    funcs = {
        "entropy": tools.entropy,  # 熵函数
        "EAV": tools.EAV,  # EAV点锐度算法函数
        "SMD": tools.SMD,  # SMD（灰度方差）函数
        "SMD2": tools.SMD2,  # SMD2 （灰度方差乘积）函数
        "variance": tools.variance,  # 方差函数
        "tenengrad": tools.tenengrad,  # Tenengrad 梯度函数
        "tenengrad2": tools.tenengrad2, # Tenengrad2 原项目实现的Tenengrad 梯度函数
        "vollath": tools.vollath, # Vollath函数
        "laplacian": tools.laplacian, # Laplacian 梯度函数
        "brenner": tools.brenner, # Brenner 梯度函数
        "nrss": tools.nrss, # NRSS 梯度结构相似度
        "energy": tools.energy, # 能量梯度函数
        "NR_IQA": tools.NR_IQA, # No-Reference Image Quality Assessment  using Blur and Noise
        "JPEQ": tools.JPEQ, # No-Reference Perceptual Quality Assessment of JPEG Compressed Images
        "JPEQ2": tools.JPEQ2 # No-Reference Image Quality Assessment forJPEG/JPEG2000 Coding
    }

    dict = {"images": []}
    for image in images:
        dict["images"].append(os.path.split(image)[-1])
        try:
            for key, val in funcs.items():

                start = time.time()
                score = val(image)
                end = time.time()

                t = end - start

                print(image, key, score, t)
                if key not in dict:
                    dict[key] = [score]
                    dict["{}_{}".format(key, "time")] = [t]
                else:
                    dict[key].append(score)
                    dict["{}_{}".format(key, "time")].append(t)
        except Exception as e:
            print(e)
    df = pd.DataFrame(dict)
    df.to_csv(os.path.join(root, "outputs", "blur_test_.csv"), index=False)


if __name__ == '__main__':
    get_clarity(os.path.join(data,"blur","images_list.txt"))